// my/pages/consultEvaluation/consultEvaluation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultant: {
      name: "王医生",
      avatar: "/static/images/doctor-avatar.png",
      title: "主任医师",
      hospital: "北京协和医院",
      department: "心理科"
    },
    consultInfo: {
      time: "2023年5月15日 14:30-15:30",
      type: "视频咨询",
      duration: "60分钟"
    },
    rating: 5, // 默认评分
    content: "", // 评价内容
    tags: [
      { id: 1, name: "专业负责", selected: false },
      { id: 2, name: "耐心细致", selected: false },
      { id: 3, name: "回复及时", selected: false },
      { id: 4, name: "解答详细", selected: false },
      { id: 5, name: "态度友好", selected: false },
      { id: 6, name: "医术精湛", selected: false }
    ],
    isAnonymous: false, // 是否匿名评价
    imageList: [], // 上传的图片列表
    maxImageCount: 3 // 最大上传图片数量
  },

  // 选择评分
  changeRating(e) {
    const rating = e.currentTarget.dataset.rating;
    this.setData({
      rating: rating
    });
  },

  // 输入评价内容
  inputContent(e) {
    this.setData({
      content: e.detail.value
    });
  },

  // 选择标签
  toggleTag(e) {
    const index = e.currentTarget.dataset.index;
    const tags = this.data.tags;
    tags[index].selected = !tags[index].selected;
    this.setData({
      tags: tags
    });
  },

  // 切换匿名评价
  toggleAnonymous() {
    this.setData({
      isAnonymous: !this.data.isAnonymous
    });
  },

  // 选择图片
  chooseImage() {
    const that = this;
    const count = this.data.maxImageCount - this.data.imageList.length;
    
    if (count <= 0) {
      wx.showToast({
        title: '最多上传' + this.data.maxImageCount + '张图片',
        icon: 'none'
      });
      return;
    }
    
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
          imageList: that.data.imageList.concat(res.tempFilePaths)
        });
      }
    });
  },

  // 预览图片
  previewImage(e) {
    const current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    });
  },

  // 删除图片
  deleteImage(e) {
    const index = e.currentTarget.dataset.index;
    const imageList = this.data.imageList;
    imageList.splice(index, 1);
    this.setData({
      imageList: imageList
    });
  },

  // 提交评价
  submitEvaluation() {
    // 获取选中的标签
    const selectedTags = this.data.tags.filter(tag => tag.selected).map(tag => tag.name);
    
    // 构建评价数据
    const evaluationData = {
      consultantId: "doctor123", // 假设的咨询师ID
      rating: this.data.rating,
      content: this.data.content,
      tags: selectedTags,
      isAnonymous: this.data.isAnonymous,
      images: this.data.imageList,
      createTime: new Date().getTime()
    };
    
    console.log('提交的评价数据：', evaluationData);
    
    // 这里应该是提交评价的API调用
    // 模拟提交成功
    wx.showLoading({
      title: '提交中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '评价提交成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          // 延迟返回上一页
          setTimeout(() => {
            wx.navigateBack();
          }, 2000);
        }
      });
    }, 1500);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})